package com.ddone.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ddone.enums.Log;
import com.ddone.enums.OperateType;
import com.ddone.enums.OperatorType;
import com.ddone.exception.AuthManagerException;
import com.ddone.model.auth.SysRole;
import com.ddone.model.vo.Result;
import com.ddone.model.vo.SysRoleQueryVo;
import com.ddone.service.SysRoleService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author ddone
 * @date 2024/3/26-23:57
 */
@RestController
@RequestMapping("/sys-role")
@Api(tags = "角色管理")
public class SysRoleController {
    @Autowired
    private SysRoleService sysRoleService;


    @Log(title = "查询所有角色",operatorType = OperatorType.MANAGE,operateType = OperateType.OTHER)
    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @ApiOperation("查询所有角色")
    @GetMapping
    public Result<List<SysRole>> all() {
        return Result.ok(sysRoleService.list());
    }


    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @ApiOperation("获取分页信息")
    @GetMapping({"/{page}/{size}"})
    public Result<IPage<SysRole>> page(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable("page") Long page,

            @ApiParam(name = "size", value = "每页显示的行数", required = true)
            @PathVariable("size") Long size,

            @ApiParam(name = "roleQueryVO", value = "查询对象", required = false)
                    SysRoleQueryVo roleQueryVo
    ) {
        Page<SysRole> rolePage = new Page<>(page, size);
        // LambdaQueryWrapper<SysRole> queryWrapper = new LambdaQueryWrapper<SysRole>()
        //         .like(StringUtils.isNotBlank(roleQueryVo.getRoleName()),
        //                 SysRole::getRoleName, roleQueryVo.getRoleName())
        //         .orderByDesc(SysRole::getId);
        // return Result.ok(sysRoleService.page(rolePage, queryWrapper));

        return Result.ok(sysRoleService.queryPage(rolePage, roleQueryVo));
    }



    @GetMapping("/{id}")
    @ApiOperation("按照角色ID获取角色")
    public Result<SysRole> get(@ApiParam(name = "id", value = "角色ID", required = true)
                               @PathVariable("id") String id) {
        return Result.ok(sysRoleService.getById(id));
    }

    @Log(title = "新增角色",operatorType = OperatorType.MANAGE,operateType = OperateType.INSERT,isSaveRequestData = true)
    @PreAuthorize("hasAuthority('bnt.sysRole.add')")
    @PostMapping
    @ApiOperation("新增角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleName", value = "角色名称", required = true),
            @ApiImplicitParam(name = "roleCode", value = "角色Code", required = true),
            @ApiImplicitParam(name = "description", value = "角色描述", required = true),

    })
    public Result<SysRole> save(@RequestBody @Validated @ApiParam("保存的角色") SysRole role) {
        sysRoleService.save(role);
        return Result.ok();
    }

    @Log(title = "新增角色",operatorType = OperatorType.MANAGE,operateType = OperateType.UPDATE,isSaveRequestData = true)
    @PreAuthorize("hasAuthority('bnt.sysRole.update')")
    @PutMapping
    @ApiOperation("修改角色")
    public Result<SysRole> update(@RequestBody @Validated @ApiParam("保存的角色") SysRole role) {
        sysRoleService.updateById(role);
        return Result.ok();
    }

    @Log(title = "删除单个",operatorType = OperatorType.MANAGE,operateType = OperateType.INSERT,isSaveRequestData = true)
    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @DeleteMapping("/{id}")
    @ApiOperation("按照ID删除角色")
    public Result<SysRole> remove(@ApiParam(name = "id", value = "删除角色", required = true)
                                  @PathVariable String id) {
        sysRoleService.removeById(id);
        return Result.ok();
    }

    @Log(title = "批量删除",operatorType = OperatorType.MANAGE,operateType = OperateType.INSERT,isSaveRequestData = true)
    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @DeleteMapping("/batch")
    @ApiOperation("批量删除数据")
    public Result<SysRole> removeBatch(@ApiParam(name = "idList", value = "删除的ID列表", required = true)
                                       @RequestBody List<Long> idList) {
        sysRoleService.removeByIds(idList);
        return Result.ok();
    }

    @GetMapping("/error")
    @ApiOperation("测试全局捕获异常")
    public Result<SysRole> error() {
        try {
            int a = 10 / 0;
        } catch (Exception e) {
            throw new AuthManagerException(20001, "出现自定义异常");
        }
        return Result.fail();
    }

    @GetMapping("/error/2")
    @ApiOperation("测试异常配置在自定义属性里面")
    public Result<Object> errorProperties() {
        throw new AuthManagerException(100001);
    }
}
